package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.PeriodGradeDate;
import com.yuyou.fn.educational.mapper.PeriodGradeDateMapper;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IPeriodGradeDateService;
import com.yuyou.fn.educational.vo.PeriodGradeDateSaveVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * PeriodGradeDate 表数据服务层接口实现类
 *
 */
@Service
public class PeriodGradeDateServiceImpl extends SuperServiceImpl<PeriodGradeDateMapper, PeriodGradeDate,Long> implements IPeriodGradeDateService {
    @Resource
    private IBaseGradeService baseGradeService;

    @Override
    public void delteByPeriodId(Long periodId) {
        Example<PeriodGradeDate> example=PeriodGradeDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("periodId",periodId);
        this.deleteByExample(example);
    }

    @Override
    public List<PeriodGradeDate> findByPeriodId(Long periodId) {
        Example<PeriodGradeDate> example=PeriodGradeDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("periodId",periodId);
        return this.findListByExample(example);
    }

    @Override
    public List<PeriodGradeDate> findByPeriodsGradeIds(List<Long> periodIds, List<Long> gradeIds) {
        Example<PeriodGradeDate> example=PeriodGradeDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("periodId",periodIds);
        if (Utils.checkListNotEmpty(gradeIds)){
            criteria.andIn("gradeId",gradeIds);
        }
        return this.findListByExample(example);
    }

    @Override
    @Transactional
    public void saveOrUpdate(Long periodId,List<PeriodGradeDateSaveVo> periodGradeDateSaveVos) {

        this.delteByPeriodId(periodId);
        List<PeriodGradeDate> periodGradeDates=new ArrayList<>(periodGradeDateSaveVos.size());
        Map<Long,Integer> record=new HashMap<>();
        for (PeriodGradeDateSaveVo periodGradeDateSaveVo:periodGradeDateSaveVos){
            if (StringUtils.isNotEmpty(periodGradeDateSaveVo.getGradeIds())){
                List<Long> gradeIds= Utils.toLongIds(periodGradeDateSaveVo.getGradeIds());
                for (Long gradeId:gradeIds){
                    if (record.containsKey(gradeId)){
                        BaseGrade baseGrade=baseGradeService.findById(gradeId);
                        throw new BusException(baseGrade.getGradeName()+"不可重复设置日期");
                    }else {
                        record.put(gradeId,1);
                    }

                    PeriodGradeDate periodGradeDate=new PeriodGradeDate();
                    periodGradeDate.setCreateTime(TimeUtils.now());
                    periodGradeDate.setUpdateTime(TimeUtils.now());
                    periodGradeDate.setGradeId(gradeId);
                    periodGradeDate.setPeriodGradeDate(periodGradeDateSaveVo.getPeriodGradeDate());
                    periodGradeDate.setPeriodId(periodId);
                    periodGradeDates.add(periodGradeDate);
                }

            }
        }
        this.insertBatch(periodGradeDates);
    }

    @Override
    public List<PeriodGradeDate> findByPeriods(List<Long> periodIds) {
        Example<PeriodGradeDate> example=PeriodGradeDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("periodId",periodIds);
        return this.findListByExample(example);
    }

    @Override
    public PeriodGradeDate findByPeriodIdGradeId(long periodId, long gradeId) {
        Example<PeriodGradeDate> example=PeriodGradeDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("periodId",periodId).andEqualTo("gradeId",gradeId);
        return this.findOneByExample(example);
    }
}